package com.example.treasureadmin.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.treasureadmin.entity.MsgEntity;
import com.example.treasureadmin.service.EssayService;
import com.example.treasureadmin.model.EssayModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/treasure-admin")
public class EssayController {

    @Autowired
    private EssayService essayService;
    private final static String[] allowFileType = new String[]{".bmp", ".jpg", ".jpeg", ".png", ".gif"};

    @DeleteMapping("/essay/{id}")
    public MsgEntity deleteEssay(@PathVariable("id") Integer id)
    {
        String code = essayService.deleteEssay(id);
        if(code.equals("200"))
            return new MsgEntity("200","SUCCESS","修改成功");
        return new MsgEntity("200","SUCCESS","文章不存在");
    }

    @GetMapping("/essay/{id}")
    public MsgEntity getEssayById(@PathVariable("id") Integer id){
        EssayModel essayById = essayService.getEssayById(id);
        return new MsgEntity("200","SUCCESS",essayById);
    }

    @GetMapping("/essay/all")
    public MsgEntity getAll()
    {
        List<EssayModel> all = essayService.getAll();
        return new MsgEntity("200","SUCCESS",all);
    }

    @GetMapping("/essay/all/{sort}")
    public MsgEntity getByTime(@PathVariable("sort") String sort)
    {
        //Timestamp createTime = requestBody.getTimestamp("createTime");
        List<EssayModel> all = essayService.getByTime(sort);
        return new MsgEntity("200","SUCCESS",all);
    }

    @PutMapping("/essay/{id}")
    public MsgEntity updateEssay(@PathVariable("id") Integer id,@RequestParam("file") MultipartFile file, @RequestParam("title") String title,
                                 @RequestParam("author") String author,@RequestParam("content") String content) throws IOException {
        if (file.isEmpty()){
            return new MsgEntity("200","ERROR","文件为空");
        }
        else{
            if (!Objects.requireNonNull(file.getOriginalFilename()).contains(".")){
                return new MsgEntity("200","ERROR","文件解析异常");
            }
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            if (suffix.isEmpty()|| !Arrays.asList(allowFileType).contains(suffix)){
                return new MsgEntity("200","ERROR","文件格式错误");
            }
            //上传到oss
            String result = essayService.uploadImage(file.getInputStream(), suffix);
            essayService.updateEssay(id,title,author,content,result);
            return new MsgEntity("200","SUCCESS","更新成功");
        }
    }

    @PostMapping("/essay/add")
    public MsgEntity addEssay(@RequestParam("file") MultipartFile file, @RequestParam("title") String title,
                              @RequestParam("author") String author,@RequestParam("content") String content) throws IOException {
        if (file.isEmpty()){
            return new MsgEntity("200","ERROR","文件为空");
        }
        else{
            if (!Objects.requireNonNull(file.getOriginalFilename()).contains(".")){
                return new MsgEntity("200","ERROR","文件解析异常");
            }
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            if (suffix.isEmpty()|| !Arrays.asList(allowFileType).contains(suffix)){
                return new MsgEntity("200","ERROR","文件格式错误");
            }
            //上传到oss
            String result = essayService.uploadImage(file.getInputStream(), suffix);
            essayService.saveEssay(title,author,content,result);
            return new MsgEntity("200","SUCCESS","更新成功");
        }
    }

    @PostMapping("/essay/upload")
    public MsgEntity updateImage(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()){
            return new MsgEntity("200","ERROR","文件为空");
        }
        else{
            if (!Objects.requireNonNull(file.getOriginalFilename()).contains(".")){
                return new MsgEntity("200","ERROR","文件解析异常");
            }
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            if (suffix.isEmpty()|| !Arrays.asList(allowFileType).contains(suffix)){
                return new MsgEntity("200","ERROR","文件格式错误");
            }
            //上传到oss
            String result = essayService.uploadImage(file.getInputStream(), suffix);
            return new MsgEntity("200","SUCCESS",result);
        }
    }
}
